package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.user.dto.ApAuthor;
import com.heima.user.dto.AuthDto;
import com.heima.user.dto.WmUser;
import com.heima.user.entity.ApUser;
import com.heima.user.entity.ApUserRealname;
import com.heima.user.feign.ArticleFeign;
import com.heima.user.feign.WeMediaFeign;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.IApUserRealnameService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.user.service.IApUserService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * APP实名认证信息表 服务实现类
 * </p>
 *
 * @author mcm
 * @since 2021-05-19
 */
@Service

public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements IApUserRealnameService {

    @Autowired
    private IApUserService iApUserService;

    @Autowired
    private ArticleFeign articleFeign;

    @Autowired
    private WeMediaFeign weMediaFeign;

    @Override
    public ResponseResult findListByStatus(AuthDto dto) {
        LambdaQueryWrapper<ApUserRealname> query = new LambdaQueryWrapper<>();
        //判断是否有参数
        if (dto.getStatus() != null) {
            query.eq(ApUserRealname::getStatus, dto.getStatus());
        }
        IPage<ApUserRealname> page = new Page<>(dto.getPage(), dto.getSize());
        IPage<ApUserRealname> iPage = this.page(page, query);
        return new PageResponseResult(dto.getPage(), dto.getSize(), iPage.getTotal(), iPage.getRecords());
    }

    @Override
    @GlobalTransactional
    public ResponseResult authPass(AuthDto dto) {
        if (dto.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApUserRealname apUserRealname = this.getById(dto.getId());
        if (apUserRealname != null) {
            apUserRealname.setStatus(9);
            this.updateById(apUserRealname);
        }
        //查询用户
        ApUser user = iApUserService.getById(apUserRealname.getUserId());
        //创建自媒体人
        WmUser wmUser = createWeMedia(user);

        if (wmUser != null) {
            //创建作者
            ApAuthor apAuthor = createArticle(user, wmUser.getId());
            if (apAuthor!=null){
                int a = 1/0;
                wmUser.setApAuthorId(apAuthor.getId());
                weMediaFeign.updateWmUser(wmUser);
                user.setFlag(1);
                iApUserService.updateById(user);
            }
        }
        return null;
    }

    private ApAuthor createArticle(ApUser user, Integer weMediaId) {
        ApAuthor apAuthor = new ApAuthor();
        apAuthor.setName(user.getName());
        apAuthor.setType(2);
        apAuthor.setUserId(user.getId());
        apAuthor.setWmUserId(weMediaId);
        apAuthor.setCreatedTime(new Date());
        ResponseResult<ApAuthor> apAuthorResponseResult = articleFeign.saveApAuthor(apAuthor);
        if (apAuthorResponseResult.getCode()==0){
            return apAuthorResponseResult.getData();
        }
        return null;
    }

    private WmUser createWeMedia(ApUser user) {
        WmUser wmUser = new WmUser();
        BeanUtils.copyProperties(user, wmUser);
        wmUser.setApUserId(user.getId());
        wmUser.setNickname(user.getName());
        wmUser.setStatus(9);
        wmUser.setType(0);
        wmUser.setCreatedTime(new Date());
        ResponseResult<WmUser> wmUserResponseResult = weMediaFeign.saveWmUser(wmUser);
        if (wmUserResponseResult.getCode() == 0) {
            WmUser wmUserDate = wmUserResponseResult.getData();
            return wmUserDate;
        }
        return null;
    }

    @Override
    public ResponseResult authFail(AuthDto dto) {
        if (dto.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApUserRealname apUserRealname = this.getById(dto.getId());
        if (apUserRealname != null) {
            apUserRealname.setStatus(2);
            apUserRealname.setReason(dto.getReason());
            this.updateById(apUserRealname);
        }
        return ResponseResult.okResult();
    }
}
